package com.woniu.market_stall_web.controller;

import com.woniu.market_stall_web.entity.Order;
import com.woniu.market_stall_web.entity.Stall;
import com.woniu.market_stall_web.entity.StallDetail;
import com.woniu.market_stall_web.entity.User;
import com.woniu.market_stall_web.mapper.OrderMapper;
import com.woniu.market_stall_web.mapper.StallDetailMapper;
import com.woniu.market_stall_web.mapper.StallMapper;
import com.woniu.market_stall_web.mapper.UserMapper;
import com.woniu.market_stall_web.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StallMapper stallMapper;
    @Autowired
    StallDetailMapper stallDetailMapper;
    @RequestMapping("/all")
    public String all(Map map, HttpSession httpSession){
        User userInfo = (User) httpSession.getAttribute("userInfo");
        List<OrderVo> list = orderMapper.select(userInfo.getId());
        //   List<OrderVo> orderList = new ArrayList<>();
       // OrderVo orderVo = new OrderVo();
//        orderVo.setOrderId(orderVo.getOrderId());
//        orderVo.setCash(orderVo.getCash());
//        orderVo.setOrderPrice(orderVo.getOrderPrice());
//        orderVo.setStatusPay(orderVo.getStatusPay());
    //    orderList.add(orderVo);
        System.out.println(list);
        map.put("orderList",list);
        return "uc";
    }

    @RequestMapping("/no")
    public String no(Map map,HttpSession httpSession){
        User userInfo = (User) httpSession.getAttribute("userInfo");
        List<OrderVo> list = orderMapper.selectOrderToBePay(userInfo.getId());
        map.put("noPay",list);
        return "no";
    }

    @RequestMapping("/already")
    public String already(Map map,HttpSession httpSession){
        User userInfo = (User) httpSession.getAttribute("userInfo");
        List<OrderVo> list = orderMapper.selectOrderAlreadyPay(userInfo.getId());
        map.put("alreadyPay",list);
        return "already";
    }

    @RequestMapping("/rentStall")
    @ResponseBody
    public String rentStall(String username,int stallId){
        System.out.println(username+"--------"+stallId);
        User user =userMapper.selectUserByName(username);
        Stall stall=stallMapper.selectByPrimaryKey(stallId);
        Order rentOrder =new Order();
        rentOrder.setOrderId(System.currentTimeMillis()+"");
        rentOrder.setStallId(stallId);
        rentOrder.setMarketId(stall.getMarketId());
        rentOrder.setUserId(user.getId());
        rentOrder.setStatusPay(1);
        rentOrder.setIsDelete(0);
        rentOrder.setOrderPrice(stall.getRentPrice());
        orderMapper.insert(rentOrder);

        stall.setEndTime(new Date());
        stall.setStatus(1);
        stall.setUserId(user.getId());
        stallMapper.updateByPrimaryKey(stall);

        StallDetail stallDetail =new StallDetail();
        stallDetail.setStallId(stallId);
        stallDetail.setStatus(0);
        stallDetail.setIsDelete(0);
        stallDetail.setRentData(new Date());
        stallDetail.setEndTime(new Date());
        stallDetail.setRentPrice(stall.getRentPrice());
        stallDetail.setUserId(user.getId());
        stallDetailMapper.insert(stallDetail);

        return null;
    }
}
